{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 字典\n",
    "# key value\n",
    "# key不能重复\n",
    "# value 可以重复\n",
    "## 1. 定义字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '张三', 'age': 18, 'gender': '男'}\n"
     ]
    }
   ],
   "source": [
    "dict1 = {}\n",
    "dict1[\"name\"]=\"张三\"\n",
    "dict1[\"age\"]=18\n",
    "dict1[\"gender\"]=\"男\"\n",
    "print(dict1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '李四', 'age': 18, 'gender': '女'}\n"
     ]
    }
   ],
   "source": [
    "dict2 = {'name': '李四', 'age': 18, 'gender': '女'}\n",
    "print(dict2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '曹操', 'age': 18}\n"
     ]
    }
   ],
   "source": [
    "dict3 = dict(name=\"曹操\",age=18)\n",
    "print(dict3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '孙权', 'age': '18'}\n"
     ]
    }
   ],
   "source": [
    "dict3 = dict([(\"name\",\"孙权\"),(\"age\",\"18\")])\n",
    "print(dict3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 练习创建一个字典保存商品信息\n",
    "# 商品名 小米手机10\n",
    "# 价格 2888\n",
    "# 颜色 红色"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': '小米手机12', 'price': 2888, 'color': '红色'}\n"
     ]
    }
   ],
   "source": [
    "goods = {'name':'小米手机12','price':2888,'color':'红色'}\n",
    "print(goods)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'张三': 57, '李四': 77, '王五': 95, '刘备': 98}\n"
     ]
    }
   ],
   "source": [
    "maths = {\"张三\":98,\"李四\":77,\"王五\":95,\"刘备\":98,\"张三\":57}\n",
    "print(maths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\n"
     ]
    }
   ],
   "source": [
    "maths[\"张三\"]=100\n",
    "print(maths[\"张三\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "99\n"
     ]
    }
   ],
   "source": [
    "maths[\"曹操\"]=99\n",
    "print(maths[\"曹操\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'孙权'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[12], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m \u001b[38;5;28mprint\u001b[39m(maths[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m孙权\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n",
      "\u001b[1;31mKeyError\u001b[0m: '孙权'"
     ]
    }
   ],
   "source": [
    "print(maths[\"孙权\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "98\n"
     ]
    }
   ],
   "source": [
    "print(maths.get(\"孙权\",\"不存在这个学生\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不存在这个学生\n"
     ]
    }
   ],
   "source": [
    "print(maths.pop(\"刘备\",\"不存在这个学生\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('曹操', 99)\n"
     ]
    }
   ],
   "source": [
    "print(maths.popitem())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'张三': 100, '李四': 77, '王五': 95}\n"
     ]
    }
   ],
   "source": [
    "print(maths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('王五', 95)\n",
      "{'张三': 100, '李四': 77}\n"
     ]
    }
   ],
   "source": [
    "print(maths.popitem())\n",
    "print(maths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'李四': 77}\n"
     ]
    }
   ],
   "source": [
    "del maths[\"张三\"]\n",
    "print(maths)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "del maths"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 遍历字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'张三': 57, '李四': 77, '王五': 95, '刘备': 98}\n",
      "张三 57\n",
      "李四 77\n",
      "王五 95\n",
      "刘备 98\n",
      "57\n",
      "77\n",
      "95\n",
      "98\n",
      "('张三', 57)\n",
      "('李四', 77)\n",
      "('王五', 95)\n",
      "('刘备', 98)\n",
      "('刘备', 98)\n",
      "('刘备', 98)\n",
      "('刘备', 98)\n",
      "('刘备', 98)\n"
     ]
    }
   ],
   "source": [
    "maths = {\"张三\":98,\"李四\":77,\"王五\":95,\"刘备\":98,\"张三\":57}\n",
    "print(maths)\n",
    "\n",
    "# 遍历方式1 通过keys()\n",
    "for k in maths.keys():\n",
    "    print(k,maths[k])\n",
    "\n",
    "# 遍历方式2 通过values()\n",
    "for v in maths.values():\n",
    "    print(v)\n",
    "\n",
    "# 遍历方式3 通过键值对\n",
    "for kv in maths.items():\n",
    "    print(kv)\n",
    "\n",
    "for k,v in maths.items():\n",
    "    print(k,v)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
